<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    class Login_m extends CI_Model {
        public function __construct() {
            //$this->load->database();
        }
        function validate_user($username, $password) {
            $this->db->select('user.*, role.*');
            $this->db->from('role');
            $this->db->join('user_role', 'role.role_id = user_role.role_id');
            $this->db->join('user', 'user_role.user_id = user.user_id');
            $this->db->where('idno', $username);
            $this->db->where('password', sha1($password));
            $query = $this->db->get();
            
            if ($query->num_rows() == 1) {
                    
                $result = $query->row_array();
                $session_data = array(
                    'user_id' => $result['user_id'],
                    'idno' => $result['idno'],
                    'first_name' => $result['fname'],
                    'middle_name' => $result['mname'],
                    'last_name' => $result['lname'],
                    'email_address' => $result['email'],
                    'password' => $result['password'],
                    'role' => $result['rolename'],
                    'logged_in' => TRUE,
                    'role_id' => $result['role_id'],
                    'department' => $result['department'],
                    'course' => $result['course']
                );
                $this->session->set_userdata($session_data);

                return $result['rolename'];
            }
            return FALSE;
        }
    }
?>
